package com.nikmesoft.android.alwaysontime.daos;


import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.util.Log;
import com.nikmesoft.android.alwaysontime.models.SmsMainItem;

public class SmsMainDAO extends BaseDAO
{	private static SmsMainDAO instance;
	private Context context;

	public SmsMainDAO(Context context)
	{	super(context);
		this.context = context;
	}

	public static void initInstance(Context context)
	{	if (instance == null)
		{// Create the instance
			instance = new SmsMainDAO(context);
		}
	}

	public static SmsMainDAO getInstance()
	{	// Return the instance
		return instance;
	}

	public ArrayList<SmsMainItem> getSms( int SmsType)
	{	openDataBase();
		ArrayList<SmsMainItem> smsList = new ArrayList<SmsMainItem>();
		// sua cau lenh truy van de lay dung sms
		//String query = "SELECT * FROM SMS;";
		String query="";
		switch (SmsType)
		{	case 0 : //all
				query = "SELECT * FROM SMS WHERE Over = 'false';";
				break;
			case 1 : //oneTime
				query = "SELECT * FROM SMS WHERE Over = 'false' AND OneTime = 'true';";
				break;
			case 2 : //schedule
				query = "SELECT * FROM SMS WHERE Over = 'false' AND OneTime = 'false';";
				break;
			case 3 : // over
				query = "SELECT * FROM SMS WHERE Over = 'true';";
				break;	
		}
		
		ArrayList<ArrayList<String>> stringList = selectRecordsFromDBList(query, null);
		close();
		//sau khi da lay ra list string, tao lai mang smsItem de return
		for (int i = 0; i < stringList.size(); i++)
		{	ArrayList<String> list = stringList.get(i);
			SmsMainItem item = new SmsMainItem();
			try
			{	// set data for item
				item.setId(Integer.parseInt(list.get(0)));
				item.setOver(Boolean.parseBoolean(list.get(1)));
				item.setPhone(list.get(2));
				item.setContent(list.get(3));
				item.setOneTime( Boolean.parseBoolean(list.get(4)));
				item.setTime(list.get(5));
				item.setDate(list.get(6));
				item.setWeekDays( list.get(7));
				item.setSound( Integer.parseInt(list.get(8)));
			}
			catch (Exception e)
			{	Log.i(context.getClass().getSimpleName(), e.getMessage());
			}
			smsList.add(item);
		}
		return smsList;
	}

	public int addSMS(SmsMainItem item)
	{	int newID;
		ContentValues values = createContentValue(item);
		openDataBase();
		newID = (int) insertRecordsInDB("SMS", null, values);
		close();
		return newID;
	}
	
	public void deleteSMSByID(int id)
	{	openDataBase();
		deleteRecordInDB("SMS", "id=?", new String[] { String.valueOf(id) });
		close();
	}

	public void updateSMSByID(int id, SmsMainItem item)
	{	ContentValues values = createContentValue(item);
		openDataBase();
		updateRecordInDB("SMS", values, "id=?", new String[] { String.valueOf(id)} );
		close();
	}
	
	public ContentValues createContentValue( SmsMainItem item)
	{	ContentValues values = new ContentValues();
		values.put("Over", item.isOver()? "true" : "false");
		values.put("Phone", item.getPhone());
		values.put("Content", item.getContent());
		values.put("OneTime", item.isOneTime()? "true" : "false");
		values.put("Time", item.getTime());
		values.put("Date", item.getDate());
		values.put("Weekdays", item.getWeekDays());
		values.put("Sound", item.getSound());
		return values;
	}
}
